﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace MainClient
{
    public partial class frmFoodInfo : Form
    {
        private string strFoodID;
        private int oper;

        //private bool isTableNameValid = true;
        //private bool isTablePriceValid = true;

        public const int OPER_ADDFOOD = 1;
        public const int OPER_RMFOOD = 2;
        public const int OPER_UPDFOOD = 3;

        public frmFoodInfo(string strFoodID, int oper)
        {
            InitializeComponent();
            this.strFoodID = strFoodID;
            this.oper = oper;
        }

        private void frmFoodInfo_Load(object sender, EventArgs e)
        {
            DataAccess.MenuDAO menuDAO = new DataAccess.MenuDAO();
            Hashtable ht = (Hashtable)menuDAO.getFood(strFoodID);

            this.txt_foodID.Text = ht["foodID"].ToString().Trim();
            this.txt_foodName.Text = ht["foodName"].ToString().Trim();
            this.cmb_foodType.Text = ht["foodType"].ToString().Trim();
            this.cmb_foodUnit.Text = ht["foodUnit"].ToString().Trim();
            this.txt_foodPrice.Text = ht["price"].ToString().Trim();
            this.txt_foodDiscount.Text = ht["discount"].ToString().Trim();
            this.txt_intro.Text = ht["intro"].ToString().Trim();

            switch (this.oper)
            {
                case OPER_ADDFOOD:
                    this.btn_oper.Text = "添加";
                    this.txt_foodID.ReadOnly = false;
                    this.txt_foodName.ReadOnly = false;
                    this.txt_foodPrice.ReadOnly = false;
                    this.cmb_foodType.Enabled = true;
                    this.cmb_foodUnit.Enabled = true;
                    this.txt_foodDiscount.ReadOnly = false;
                    this.txt_intro.ReadOnly = false;
                    break;
                case OPER_RMFOOD:
                    this.btn_oper.Text = "移除";
                    this.txt_foodID.ReadOnly = true;
                    this.txt_foodName.ReadOnly = true;
                    this.txt_foodPrice.ReadOnly = true;
                    this.cmb_foodType.Enabled = false;
                    this.cmb_foodUnit.Enabled = false;
                    this.txt_foodDiscount.ReadOnly = true;
                    this.txt_intro.ReadOnly = true;
                    break;
                case OPER_UPDFOOD:
                    this.btn_oper.Text = "修改";
                    this.txt_foodID.ReadOnly = true;
                    this.txt_foodName.ReadOnly = true;
                    this.txt_foodPrice.ReadOnly = false;
                    this.cmb_foodType.Enabled = true;
                    this.cmb_foodUnit.Enabled = true;
                    this.txt_foodDiscount.ReadOnly = false;
                    this.txt_intro.ReadOnly = false;
                    break;
                default:
                    this.Close();
                    break;
            }
        }

        private void btn_oper_Click(object sender, EventArgs e)
        {
            DataAccess.MenuDAO menuDAO = new DataAccess.MenuDAO();
            string strShow;
            switch (this.oper)
            {
                case OPER_ADDFOOD:
                    if (menuDAO.isFoodExist(this.txt_foodID.Text.Trim()))
                    {
                        strShow = "添加新菜品" + this.txt_foodID.Text.Trim() + "失败，原因：该菜品已存在！";
                        MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    menuDAO.addOrUpdateFood(
                                        LoginUser.getCurrOper(),
                                        this.txt_foodID.Text.Trim(),
                                        this.txt_foodName.Text.Trim(),
                                        this.cmb_foodType.Text.Trim(),
                                        this.cmb_foodUnit.Text.Trim(),
                                        double.Parse(this.txt_foodPrice.Text),
                                        float.Parse(this.txt_foodDiscount.Text),
                                        this.txt_intro.Text.Trim()
                                        );
                    break;
                case OPER_RMFOOD:
                    if (menuDAO.isFoodExist(this.txt_foodID.Text.Trim()))
                    {
                        menuDAO.removeFood(LoginUser.getCurrOper(), this.txt_foodID.Text.Trim());
                    }
                    else
                    {
                        strShow = "删除新菜品" + this.txt_foodID.Text.Trim() + "失败，原因：该菜品已存在！";
                        MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    break;
                case OPER_UPDFOOD:
                    if (!menuDAO.isFoodExist(this.txt_foodID.Text.Trim()))
                    {
                        strShow = "更新菜品" + this.txt_foodID.Text.Trim() + "失败，原因：该菜品不存在！";
                        MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    menuDAO.addOrUpdateFood(
                                        LoginUser.getCurrOper(),
                                        this.txt_foodID.Text.Trim(),
                                        this.txt_foodName.Text.Trim(),
                                        this.cmb_foodType.Text.Trim(),
                                        this.cmb_foodUnit.Text.Trim(),
                                        double.Parse(this.txt_foodPrice.Text),
                                        float.Parse(this.txt_foodDiscount.Text),
                                        this.txt_intro.Text.Trim()
                                        );
                    break;
                default:
                    this.Close();
                    break;
            }
        } // end of function

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }// end of function
    } // end of class
}
